package com.chencs.coupon.hwc;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class HWCUtils {

    public static HWCRootBean getHWCData(String code){
      String requestStr ="{\n" +
              "\t\"version\": \"1.0\",\n" +
              "\t\"app\": \"NBYHQ\",\n" +
              "\t\"content\": \""+code+"\",\n" +
              "\t\"imei\": \"ABB9D281-CA72-4ADB-B7EB-2311BF02D9CE\",\n" +
              "\t\"platform\": \"1\"\n" +
              "}";
        HWCRootBean hwcRootBean = null;
        try{
            PostMethod getMethod = new PostMethod("https://api.huiwancun.net/HwcWxappService/taobaoapp/parseProduct.api");
            RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
            getMethod.setRequestEntity(entity);
            getMethod.setRequestHeader("Content-Type","application/json;charset=UTF-8");
            new HttpClient().executeMethod(getMethod);
            InputStream inputStream = getMethod.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String str= "";
            while((str = br.readLine()) != null){
                stringBuffer .append(str);
            }
            String str2 = stringBuffer.toString();
             hwcRootBean = JSONObject.parseObject(str2,HWCRootBean.class);
    }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        return  hwcRootBean;
    }
    
//    public static void main(String[] args) {
//        HWCUtils.getHWCData("44CQRUxFRlQgNETooYzotbDpnovlnqvov5Dliqjlh4/pnIfpgI/msJTlkLjmsZfpnovlnqvnlLflpbPoiJLpgILlvpfkuIrnmL7jgJHvvIzlpI3liLbov5nmnaHkv6Hmga/vv6VwcVRjMHVRNTJPTO+/peWQjuaJk+W8gOaJi+a3mA==");
//    }
}
